import saleOrdersAPI from '../../../common/api/breedsales/orderbills.api.js'
import doT from '../../../../vendor/template/doT.js';

(function (window, document, $) {
    let _vm, getEntryInfo =[];
    const _json = Utils.ParsingUrl(window.location);
    const vehicleTemplate = doT.template($('#vehicle-template').text());

    const LICENSE_PLATE = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/;

    $(function () {
        _vm = new Vue({
            el: "#vehicle-form",
            data() {
                return {
                    "orderId": "",
                    "orderEntreiId": 0,
                    "deiverId": "",
                    "deiverName": "",
                    "deiverNumCode": "",
                    "deiverPhone": "",
                    "carNo": ""
                }
            },
            methods: {
                submit() {
                //    savedata()
                }
            },
        });
        $('body').attr('data-weui-theme', 'light'); //强制光明模式
        _initCommand()
    });

    //命令初始化
    const _initCommand = () => {
        getEntryInfo = JSON.parse(Utils.GetSessionStorage("getEntryInfo"));
        $('#vehicleorders').empty().append(vehicleTemplate(getEntryInfo));

        if (Utils.IsNotNull(_json.carNo)) {
            $('#carNo').val(_json.carNo);
            $('#deiverName').val(_json.deiverName);
            $('#deiverPhone').val(_json.deiverPhone);
        }

        //提交
        $('#informationSubmit').on('click', function () {
            //电话号码
            const PHONE_VERIFICATION = /^1[3456789]\d{9}$/;
            if (!Utils.IsNotNull($(`#deiverName`).val())) {
                return $.toptip(`司机姓名不能为空!`, 'warning');
            }
            if (!Utils.IsNotNull($(`#deiverPhone`).val())) {
                return $.toptip(`司机电话号码不能为空!`, 'warning');
            }
            if (!(PHONE_VERIFICATION.test(($('#deiverPhone').val())))) {
                return $.toptip(`请输入正确的电话号码!`, 'warning');
            }
            if (!Utils.IsNotNull($(`#carNo`).val())) {
                return $.toptip(`车牌号不能为空!`, 'warning');
            }
            let carNo = $(`#carNo`).val().toUpperCase();
            if (!carNo.match(LICENSE_PLATE)) {
                return $.toptip(`请输入正确的车牌号!`, 'warning');
            }
            let param = {
                "orderId": _json.orderId,
                "orderEntreiId": _json.seq,
                "deiverId": '',
                "deiverName": $('#deiverName').val(),
                "deiverNumCode": '',
                "deiverPhone": $('#deiverPhone').val(),
                "carNo": $('#carNo').val()
            }
            saleOrdersAPI.putVehicle(param).then(res => {
                $.toptip(res, 'success');
                window.history.go(-1);
                //location.href = `./saleorders.html?v=${Utils.GetTime()} `;
            });
        });
        //取消
        $('#returnMain').on('click', function () {
            window.history.go(-1);
        });
    }

})(window, document, jQuery)